package com.bytedance.common.plugin.interfaces.pushmanager.classhandle;

import android.content.Context;
import android.support.design.a;

import com.bytedance.common.utility.Logger;

import dalvik.system.PathClassLoader;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Map;

public class PushClassLoader extends PathClassLoader {
    public interface ICreate {
        PushClassLoader create(ClassLoader arg1);
    }

    protected static ICreate sCreate;
    protected static Map<String, String> sProtectClassMap;

    static {
        LinkedHashMap v0 = new LinkedHashMap();
        PushClassLoader.sProtectClassMap = new LinkedHashMap();
        PushClassLoader.sProtectClassMap.put("com.xiaomi.mipush.sdk.PushMessageHandler", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.xiaomi.mipush.sdk.MessageHandleService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.xiaomi.push.service.receivers.NetworkStatusReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.xiaomi.push.service.receivers.MIPushMessageHandler", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.taobao.accs.data.MsgDistributeService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("org.android.agoo.accs.AgooService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.umeng.message.UmengIntentService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.taobao.agoo.AgooCommondReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.umeng.message.UmengMessageCallbackHandlerService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.umeng.message.UmengMessageBootReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.PushEventReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.PushBootReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.PushService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.huawei.push.service.receivers.HWPushMessageHandler", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.ss.android.message.MessageReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.ss.android.message.NotifyService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.ss.android.message.NotifyService", "com.ss.android.message.NotifyService");
        PushClassLoader.sProtectClassMap.put("com.ss.android.message.PushJobService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.meizu.cloud.pushsdk.NotificationService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.meizu.cloud.pushsdk.SystemReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.ss.android.article.lite.MzPushMsgReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.ss.android.newmedia.redbadge.RedbadgeHandler", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.ss.android.http.OpenUrlService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.xiaomi.push.service.XMPushService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.xiaomi.push.service.XMJobService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.xiaomi.push.service.receivers.PingReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.taobao.accs.ChannelService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.taobao.accs.EventReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.taobao.accs.ServiceReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.taobao.accs.ChannelService$KernelService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.umeng.message.UmengMessageIntentReceiverService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.umeng.UmengMessageHandler", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.SystemEventReceiver", "com.alibaba.sdk.android.push.SystemEventReceiver");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.PushEventReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.PushBootReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.PushService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.ss.android.message.NotifyIntentService", "com.ss.android.message.NotifyIntentService");
        PushClassLoader.sProtectClassMap.put("com.huawei.android.pushagent.permission.PermissionsMgrActivity", "com.huawei.android.pushagent.permission.PermissionsMgrActivity");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.AliyunPushIntentService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.MsgService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.channel.TaobaoRecvService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.SystemEventReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.aliyun.AliyunMessageReceiver", "com.ss.android.push.DefaultReceiver");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.PushIntentService", "com.ss.android.push.DefaultService");
        PushClassLoader.sProtectClassMap.put("com.alibaba.sdk.android.push.CloudPushIntentService", "com.ss.android.push.DefaultService");
    }

    public PushClassLoader(String arg1, ClassLoader arg2) {
        super(arg1, arg2);
    }

    protected static PushClassLoader createClassLoader(ClassLoader arg2) {
        PushClassLoader v0 = PushClassLoader.sCreate != null ? PushClassLoader.sCreate.create(arg2) : new PushClassLoader("PushClassLoader", arg2);
        return v0;
    }

    public static void hackClassLoader(Context arg6) {
        if (arg6 != null) {
            try {
                Object v0_1 = new Smith(a.a(Class.forName("android.app.ActivityThread"), "currentActivityThread", new Object[0]), "mPackages").get();
                Map map = (Map) v0_1;
                Iterator v2 = map.keySet().iterator();
                while (v2.hasNext()) {
                    WeakReference ref = (WeakReference) map.get(v2.next());
                    Object obj = ref.get();
                    if (obj == null) {
                        continue;
                    }
                    Smith v3 = new Smith(obj, "mClassLoader");
                    ClassLoader v1 = (ClassLoader) v3.get();
                    if (v1.getClass().getName().equals(PushClassLoader.class.getName())) {
                        continue;
                    }
                    v3.set(PushClassLoader.createClassLoader(v1));
                }

                Smith v1_1 = new Smith(new Smith(new Smith(arg6, "mBase").get(), "mPackageInfo").get(), "mClassLoader");
                v0_1 = v1_1.get();
                if (v0_1.getClass().getName().equals(PushClassLoader.class.getName())) {
                    return;
                }

                v1_1.set(PushClassLoader.createClassLoader(((ClassLoader) v0_1)));
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    public static boolean isMatchPushClassNotFound(Throwable arg7) {
        if (arg7 != null) {
            try {
                if (!"java.lang.RuntimeException".equals(arg7.getClass().getName())) {
                    label_8:
                    return false;
                }

                String v3 = arg7.getMessage().trim();
                Iterator<Entry<String, String>> v4 = PushClassLoader.sProtectClassMap.entrySet().iterator();
                do {
                    if (!v4.hasNext()) {
                        return false;
                    }

                    Entry<String,String> v0_2 = v4.next();
                    if (v0_2 == null) {
                        continue;
                    }

                    if ("com.ss.android.push.DefaultService".equals(( v0_2).getValue())) {
                        if (!v3.startsWith("Unable to instantiate service " + ((Entry) v0_2).getKey())) {
                            continue;
                        }

                        return true;
                    }

                    if ("com.ss.android.message.NotifyService".equals(( v0_2).getValue())) {
                        if (!v3.startsWith("Unable to instantiate service " + ( v0_2).getKey())) {
                            continue;
                        }

                        return true;
                    }

                    if ("com.ss.android.message.NotifyIntentService".equals(( v0_2).getValue())) {
                        if (!v3.startsWith("Unable to instantiate service " + ( v0_2).getKey())) {
                            continue;
                        }

                        return true;
                    }

                    if ("com.ss.android.push.DefaultReceiver".equals(( v0_2).getValue())) {
                        if (!v3.startsWith("Unable to instantiate receiver " + ( v0_2).getKey())) {
                            continue;
                        }

                        return true;
                    }

                    if ("com.xiaomi.push.service.receivers.NetworkStatusReceiver".equals(( v0_2).getValue())) {
                        if (!v3.startsWith("Unable to instantiate receiver " + ( v0_2).getKey())) {
                            continue;
                        }

                        return true;
                    }

                    if ("com.alibaba.sdk.android.push.SystemEventReceiver".equals(( v0_2).getValue())) {
                        if (!v3.startsWith("Unable to instantiate receiver " + ( v0_2).getKey())) {
                            continue;
                        }

                        return true;
                    }

                    if (!"com.huawei.android.pushagent.permission.PermissionsMgrActivity".equals(( v0_2).getValue())) {
                        continue;
                    }

                    if (!v3.startsWith("Unable to instantiate activity")) {
                        continue;
                    }

                    if (!v3.contains(v0_2.getKey())) {
                        continue;
                    }

                    return true;
                } while (true);
            } catch (Throwable v0) {
                v0.printStackTrace();
                return false;
            }
        }
        return false;
    }

    protected Class loadClass(String arg5, boolean arg6) throws ClassNotFoundException {
        Class v0_1;
        try {
            if (Logger.debug()) {
                Logger.d("PushClassLoader", "loadClass className = " + arg5);
            }

            v0_1 = super.loadClass(arg5, arg6);
        } catch (ClassNotFoundException v0) {
            if (Logger.debug()) {
                Logger.d("PushClassLoader", "ClassNotFoundException className = " + arg5);
            }

            if (PushClassLoader.sProtectClassMap.containsKey(arg5)) {
                return super.loadClass(PushClassLoader.sProtectClassMap.get(arg5), arg6);
            }

            throw v0;
        }

        return v0_1;
    }

    protected static void setCreate(ICreate arg0) {
        PushClassLoader.sCreate = arg0;
    }
}

